package com.aj.easylibrary.net.rx;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.text.TextUtils;
import android.util.Log;

import com.aj.easylibrary.exception.ApiException;
import com.aj.easylibrary.net.bean.BaseResponse;

import java.io.IOException;

import io.reactivex.Observer;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import retrofit2.HttpException;

public abstract class RxSubcriber<T> implements Observer<BaseResponse<T>> {

    private ProgressDialog mProgressDialog;
    private Disposable disposable;
    private Activity mActivity;
    private String errorMsg;

    public RxSubcriber(Activity activity) {
        this.mActivity = activity;
    }


    @Override
    public void onSubscribe(Disposable d) {
        disposable = new CompositeDisposable();
        showLoading();
    }

    @Override
    public void onNext(BaseResponse<T> value) {
        if (!value.isSuccess()) {
            ApiException apiException = new ApiException(value.getCode(), value.getMsg());
            onError(apiException);
            return;
        }
        onSuccess(value.getData());
    }


    @Override
    public void onError(Throwable e) {
        Log.e("RxSubcriber", "error", e);
        if (e instanceof IOException) {
            /** 没有网络 */
            errorMsg = "Please check your network status";
        } else if (e instanceof HttpException) {
            /** 网络异常，http 请求失败，即 http 状态码不在 [200, 300) 之间, such as: "server internal error". */
            errorMsg = ((HttpException) e).response().message();
        } else if (e instanceof ApiException) {
            /** 网络正常，http 请求成功，服务器返回逻辑错误 */
            errorMsg = ((ApiException) e).getMsg();
        } else {
            /** 其他未知错误 */
            errorMsg = !TextUtils.isEmpty(e.getMessage()) ? e.getMessage() : "unknown error";
        }


        dismissLoading();

        new AlertDialog.Builder(mActivity)
                .setTitle("提示")
                .setMessage(errorMsg)
                .setPositiveButton("确定", (dialogInterface, i) -> {
                    dialogInterface.dismiss();
                    if (!disposable.isDisposed()) {
                        disposable.dispose();
                    }
                }).show();
    }

    @Override
    public void onComplete() {
        dismissLoading();
        Log.d("print", "-->执行了完成的方法");
    }

    public abstract void onSuccess(T t);

    private void showLoading() {
        if (mProgressDialog == null) {
            mProgressDialog = new ProgressDialog(mActivity);
            mProgressDialog.setMessage("正在加载中...");
            mProgressDialog.show();
        }
    }

    private void dismissLoading() {
        if (mProgressDialog != null) {
            mProgressDialog.dismiss();
        }
    }

}
